package cn.itcast.bos.web.action.base;

import java.util.HashMap;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
		//模拟驱动
	private Standard standard = new Standard();
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}
	//注解Service对象
	@Autowired
	private StandardService standardService;
	
	//添加操作
	@Action(value="standard_save",
			results={@Result(name="success",type="redirect",
				location="./pages/base/standard.html")})
	public String save(){
		System.out.println("添加派件");
		standardService.save(standard);
		return SUCCESS;
	}
	 //属性驱动
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

	//分页列表查询
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//调用业务层,查询数据结果
		Pageable pageable = new  PageRequest(page-1, rows);
		Page<Standard> pageDate = standardService.findPageDate(pageable);
		//返回客户端数据total和rows
		HashMap<String, Object> result = new HashMap<String,Object>();
		result.put("total", pageDate.getTotalElements());
		result.put("rows", pageDate.getContent());
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
